*** Table 3 ***	

use "$data/simulated_df.dta", clear 

********************************************************************************
*** Heterogeneity ***
********************************************************************************

est clear 
foreach var in any_post_total_fpds_over50k ///
	any_post_total_patent any_post_total_hi_orig  ///
	post_any_vc any_post_total_allsbir_diff  {
		
		di "`var'"
		
		if "`var'"=="any_post_total_patent" {
			local stub "pat"
			local head "Patent"
			local yr "1095"
		}
		if "`var'"=="any_post_total_fpds_over50k" {
			local stub "fpds"
			local head "Non-SBIR Contract"
		}
		if "`var'"=="post_any_vc" {
			local stub "vc"
			local head "VC"
			local yr "1095"
		}
		if "`var'"=="any_post_total_hi_orig" {
			local stub "orig"
			local head "High Originality"
			local yr "1095"
		}
		if "`var'"=="any_post_total_allsbir_diff" {
			local stub "allsbir"
			local yr ""
		}
		
		foreach x in age emp {
			forvalues i = 1/2 {
				eststo `stub'`x'`i': areg `var'`yr' i.select##i.open ///
				$rddmain_controls [aweight=weight] ///
				if  $open_first & any_pre_total_allsbir==0 & ///
					`x'_2tile==`i' | $conv17_first & any_pre_total_allsbir==0 ///
					&  `x'_2tile==`i', ///
				cluster(topic_n) absorb(topic_n)
				estadd scalar R= e(r2): `stub'`x'`i'
				estadd local pd "2017-19": `stub'`x'`i'
				estadd local prop "First": `stub'`x'`i'
				qui sum `var'`yr' if  $open_first & `x'_2tile==`i' | ///
					$conv17_first & `x'_2tile==`i'
				estadd scalar ymean = r(mean): `stub'`x'`i'
			}
		}
	} 

esttab fpdsage* vcage* patage* origage* allsbirage* using "$tables/table3a.tex", ///
	$general $labels ///
	keep(1.select 1.select#1.open) ///
	scalars("prop Proposal" "pd Time Period" "ymean Outcome Mean") ///
	postfoot("\midrule \end{tabular} \end{footnotesize} \\ ") ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{footnotesize} \begin{tabular}{l*{10}{>{\centering\arraybackslash}p{0.09\textwidth}}} \\ \toprule \multicolumn{11}{c}{Panel A: Heterogeneity By Age} \\ \midrule & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(DoD Contract)}} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(VC)}} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$(Patent)}} & \multicolumn{2}{c}{$\mathbbm{1}$(High Originality} & \multicolumn{2}{c}{\multirow{2}{*}{$\mathbbm{1}$($> 2$ SBIR)}}  \\ & & & & & & & \multicolumn{2}{c}{Patent)} & & \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9} \cmidrule(lr){10-11} \\ & Young & Old & Young & Old & Young & Old & Young & Old & Young & Old \\ \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7} \cmidrule(lr){8-8} \cmidrule(lr){9-9} \cmidrule(lr){10-10} \cmidrule(lr){11-11}  ")
	
esttab fpdsemp* vcemp* patemp* origemp* allsbiremp*  using "$tables/table3b.tex", ///
	$general $labels ///
	keep(1.select 1.select#1.open) ///
	scalars("prop Proposal" "pd Time Period" "ymean Outcome Mean") ///
	postfoot("\midrule \end{tabular} \end{footnotesize} \\ ") ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{footnotesize} \begin{tabular}{l*{10}{>{\centering\arraybackslash}p{0.09\textwidth}}} \toprule \multicolumn{11}{c}{Panel B: Heterogeneity By Employment} \\ \midrule & Small & Large & Small & Large & Small & Large & Small & Large & Small & Large \\ \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(lr){7-7} \cmidrule(lr){8-8} \cmidrule(lr){9-9} \cmidrule(lr){10-10} \cmidrule(lr){11-11}  ")
